//
//  StrategyDetailController.m
//  FreeTrip
//
//  Created by 邓芮 on 15/11/25.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "StrategyDetailController.h"
#import "StrategyDetailModel.h"
#import "StrategyDetailRowModel.h"
#import "PhotosModel.h"
#import "StrategyDetailCell.h"
#import "StrategyDetailHeaderView.h"

#import "PhotoViewController.h"

#import "UMSocial.h"

@interface StrategyDetailController ()<CollectionViewDelegate>

@property (nonatomic, strong) NSMutableArray *modelArray;
//@property (nonatomic, strong) NSMutableArray *rowsArray;
//@property (nonatomic, strong) NSMutableArray *photoArray;



@end

static NSString *cellIdentifier = @"cell";
static NSString *headerIdentifier = @"header";
@implementation StrategyDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    
    [self.tableView registerClass:[StrategyDetailCell class] forCellReuseIdentifier:cellIdentifier];
    
    [self.tableView registerClass:[StrategyDetailHeaderView class] forHeaderFooterViewReuseIdentifier:headerIdentifier];
    [self parsedData];
   
    
}



- (void)parsedData
{
    self.modelArray = [NSMutableArray array];
    for (NSDictionary *dic in self.array)
    {
        StrategyDetailModel *sectionModel = [[StrategyDetailModel alloc]init];
        sectionModel.title = [dic objectForKey:@"title"];
        NSArray *array = [dic objectForKey:@"sections"];
        sectionModel.sectionsArray = [NSMutableArray array];
        for (NSDictionary *sectionsDic in array)
        {
            StrategyDetailRowModel *rowsModel = [[StrategyDetailRowModel alloc]init];
            rowsModel.myDescription = [sectionsDic objectForKey:@"description"];
            rowsModel.title = [sectionsDic objectForKey:@"title"];
            rowsModel.travel_date = [sectionsDic objectForKey:@"travel_date"];
            NSDictionary *user = [sectionsDic objectForKey:@"user"];
            rowsModel.name = [user objectForKey:@"name"];
            NSArray *photoArray = [sectionsDic objectForKey:@"photos"];
            rowsModel.photo = [NSMutableArray array];
            for (NSDictionary *photoDic in photoArray)
            {
                PhotosModel *photoModel = [[PhotosModel alloc]init];
                photoModel.myDescription = [photoDic objectForKey:@"description"];
                photoModel.image_url = [photoDic objectForKey:@"image_url"];
                NSNumber *height = [photoDic objectForKey:@"image_height"];
                photoModel.image_height = [height integerValue];
                photoModel.image_width = [[photoDic objectForKey:@"image_width"] integerValue];
                [rowsModel.photo addObject:photoModel];
//                NSLog(@"%@", rowsModel.photo);
                
            }
            [sectionModel.sectionsArray addObject:rowsModel];
//            NSLog(@"%@", sectionModel.sectionsArray);
        }
        [self.modelArray addObject:sectionModel];
    }

}

#pragma mark -- collection代理方法 --
- (void)JumpPage:(NSMutableArray *)array item:(NSInteger)item
{
    PhotoViewController *photoVc = [[PhotoViewController alloc] init];
    photoVc.modelArray = array;
    photoVc.item = item;
    [self presentViewController:photoVc animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.modelArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    StrategyDetailModel *sectionModel = self.modelArray[section];
    if (sectionModel.isOpen == YES)
    {
//        NSLog(@"%ld", sectionModel.sectionsArray.count);
        return sectionModel.sectionsArray.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyDetailModel *sectionModel = self.modelArray[indexPath.section];
    NSMutableArray *rowsArray = sectionModel.sectionsArray;
    StrategyDetailRowModel *rowModel = rowsArray[indexPath.row];
    return [StrategyDetailCell heightForCell:rowModel];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StrategyDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    StrategyDetailModel *sectionModel = self.modelArray[indexPath.section];
    NSMutableArray *rowsArray = sectionModel.sectionsArray;

    cell.rowModel = rowsArray[indexPath.row];

    cell.delegate = self;
    
    return cell;
}

#pragma mark -- 表头 --
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [StrategyDetailHeaderView heightForHeaderView];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    StrategyDetailHeaderView *headerView = [[StrategyDetailHeaderView alloc]init];
    headerView.model = self.modelArray[section];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(open:)];
    [headerView addGestureRecognizer:tapGes];
    headerView.tag = section;
    return headerView;
}

- (void)open:(UITapGestureRecognizer *)tap
{
    StrategyDetailHeaderView *headerView = (StrategyDetailHeaderView *)tap.view;
    StrategyDetailModel *sectionModel = self.modelArray[headerView.tag];
    sectionModel.isOpen = !sectionModel.isOpen;
    
    [self.tableView reloadData];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
